package test.blued.com.casecontroller.myInterface;

import android.util.Log;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class HttpJson implements Runnable {

	public static  String BASIC_URL;
	public String postStr;
	public Object postObject;
	public String type;
	public Map header;
	public String responseStr;

	public HttpJson(String BASIC_URL) {
		this.type = "get";
		this.BASIC_URL = BASIC_URL;
	}

	public HttpJson(String type, String BASIC_URL, Map header) {
		this.type = type;
		this.BASIC_URL = BASIC_URL;
		this.header = header;
	}

	public HttpJson(String type, String BASIC_URL, String postStr, Map header) {
		this.type = type;
		this.BASIC_URL = BASIC_URL;
		this.postStr = postStr;
		this.header = header;
	}
	public HttpJson(String type, String BASIC_URL, Object postObject, Map header) {
		this.type = type;
		this.BASIC_URL = BASIC_URL;
		this.postObject = postObject;
		this.header = header;
	}


	public void get(){
		HttpClient httpClient = new DefaultHttpClient();
		// 设置超时
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

		HttpGet httpget = new HttpGet(BASIC_URL);
		if(header!=null && header.size()>0){
			Iterator it = header.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry ent = (Map.Entry) it.next();
				httpget.addHeader((String)ent.getKey(), (String)ent.getValue());
			}
		}
		try {
			HttpResponse response = httpClient.execute(httpget);

			//获取返回结果
			int statusCode = response.getStatusLine().getStatusCode();
			Log.i("T11", statusCode + BASIC_URL);
			if(statusCode == HttpStatus.SC_OK){
				responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
				Log.i("T11", "responseStr:" + responseStr);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void postString() {

		HttpClient httpClient = new DefaultHttpClient();
		// 设置超时
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

		HttpPost httpPost = new HttpPost(BASIC_URL);
		if(header!=null && header.size()>0){
			Iterator it = header.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry ent = (Map.Entry) it.next();
				httpPost.addHeader((String)ent.getKey(), (String)ent.getValue());
			}
		}
		try {
			StringEntity se = new StringEntity(postStr,"utf-8");
			httpPost.setEntity(se);
			HttpResponse response = httpClient.execute(httpPost);
			//获取返回结果
			int statusCode = response.getStatusLine().getStatusCode();
			Log.i("T11", statusCode + BASIC_URL);
			if(statusCode == HttpStatus.SC_OK){
				responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
				Log.i("T11", "responseStr:" + responseStr);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void postObject() {
		HttpClient httpClient = new DefaultHttpClient();
		// 设置超时
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

		HttpPost httpPost = new HttpPost(BASIC_URL);
		if(header!=null && header.size()>0){
			Iterator it = header.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry ent = (Map.Entry) it.next();
				httpPost.addHeader((String)ent.getKey(), (String)ent.getValue());
			}
		}
		try {
			Gson gson = new Gson();
			String jsonStr = gson.toJson(postObject);
			StringEntity se = new StringEntity(jsonStr,"utf-8");
			httpPost.setEntity(se);
			HttpResponse response = httpClient.execute(httpPost);
			//获取返回结果
			int statusCode = response.getStatusLine().getStatusCode();
			if(statusCode == HttpStatus.SC_OK){
				responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
				Log.i("T11", "responseStr:" + responseStr);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Object getResponseStrFile(String file, String ...args){
		JSONObject jo;
		try {
			jo = new JSONObject(responseStr);
			if(args.length == 0)
				return jo.get(file);
			else{
				jo = (JSONObject)jo.get(file);
				int i = 0;
				while(i<args.length-1){
					jo = (JSONObject)jo.get(args[i]);
					i++;
				}
				return jo.get(args[args.length-1]);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "no json!!";
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		if(type.equals("post"))
			this.postString();
		else
			this.get();
	}

}
